<?php
class group
{
	public $table;
	public $utable;
	public $pages;
	public $db;
	public $error;
	public $current_user;
	
	function group()
	{
		global $db,$current_user;
        $this->db = &$db;
        $this->table = DB_PRE.'group';
        $this->utable = DB_PRE.'admin';
        $this->current_user = $current_user;
	}
	
	/**
	 * 添加组
	 *
	 * @param array $data
	 * @return int or false
	 */
	function add_group($data)
	{
		$params = $data;
		$params['username'] = $this->current_user['username'];
		$params['inputtime'] = $params['updatetime'] = time();
		$params['status']	=	STATUS_NORMAL;
		$res = $this->db->insert($this->table,$params);
		return $res;
	}
	
	/**
	 * 编辑单条组信息
	 *
	 * @param array $data
	 * @param int $g_id
	 * @return bool
	 */
	function edit_group($data,$g_id)
	{
		$params = $data;
		$params['username'] = $this->current_user['username'];
		$params['updatetime'] = time();
		$res = $this->db->simple_update($this->table,$params,array('g_id'=>$g_id));
		return $res;
	}
	
	/**
	 * 根据id获得组信息
	 *
	 * @param int $g_id
	 * @param char $column
	 * @return array
	 */
	function get_group_by_id($g_id,$column='*')
	{
		$this->db->prepare('select '.$column.' from '.$this->table.' where g_id=? and status=?');
		$this->db->set_string($g_id);
		$this->db->set_int(STATUS_NORMAL);
		$group = $this->db->execute(true);
		return $group;
	}
	
	/**
	 * 删除组
	 *
	 * @param int $gid
	 * @return bool
	 */
	function delete_group($gid)
	{
		return $this->edit_group(array('status'=>STATUS_DEL),$gid);
	}
	
	/**
	 * 查询组
	 * 
	 * 查询后分页从 pages属性获得
	 *
	 * @param int $page
	 * @param int $pagesize
	 * @param char $name
	 * @param char $columns
	 * @return array  
	 */
	function search_group($page,$pagesize,$canview,$name,$columns = '*')
	{
		$start = ($page - 1) * $pagesize;
		if(is_array($canview))
		{
			if(sizeof($canview) <= 0)
			{
				return array();
			}
			else 
			{
				$canview = ' and g_id in(\'' . implode("','", $canview) . '\')';
			}
		}
		else 
		{
			$canview = '';
		}
		if(empty($name))
		{
			$this->db->prepare('select count(*) as nums from '.$this->table.' where status=? ' . $canview);
			$this->db->set_int(STATUS_NORMAL);
			$c_res = $this->db->execute(true);
			$this->db->prepare('select '.$columns.' from '.$this->table.' where status=? ' . $canview . ' order by listorder desc,inputtime desc limit ?,?');
			$this->db->set_int(STATUS_NORMAL);
			$this->db->set_int($start);
			$this->db->set_int($pagesize);
			$res = $this->db->execute();
		}
		else 
		{
			$this->db->prepare('select count(*) as nums from '.$this->table.' where status=? and g_name like ? ' . $canview);
			$this->db->set_int(STATUS_NORMAL);
			$this->db->set_string('%'.$name.'%');
			$c_res = $this->db->execute(true);
			$this->db->prepare('select '.$columns.' from '.$this->table.' where status=? and g_name like ? ' . $canview . ' order by listorder desc,inputtime desc limit ?,?');
			$this->db->set_int(STATUS_NORMAL);
			$this->db->set_string('%'.$name.'%');
			$this->db->set_int($start);
			$this->db->set_int($pagesize);
			$res = $this->db->execute();
		}
		$cout = $c_res['nums'];
		$this->pages = pages($cout,$page,$pagesize);
		return $res;
	}

	/**
	 * 获得所有有效阻列表
	 *
	 * @return array
	 */
    function get_groups_list()
    {
        $list = $this->db->query( 'select g_id,g_name from '.$this->table.' where status = 99' );
        $array = array();
        foreach($list as $l)
        {
            $array[$l['g_id']] = $l['g_name'];
        }
    	return $array;
    }
    
    /**
     * 根据组的权限缓存字符串，格式化出指定的权限字符串中某一资源类型的权限
     *
     * @param varchar $view_type
     * @param varchar $type_list
     * @return array
     */
    function formate_views_list($view_type,$type_list)
    {
    	if(!empty($view_type))
    	{
	    	$tmp = explode('|',$type_list);
	    	foreach ($tmp as $v)
	    	{
	    		$res = explode(':',$v);
	    		if($res[0]==$view_type)
	    		{
	    			$list = explode(',',$res[1]);
	    			return $list;
	    		}
	    	}
    	}
    	return array();
    }
}
?>